##############################################################################
#
# Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Skeleton ZopeTestCase

$Id: testSkeleton.py 30245 2005-05-05 09:50:09Z shh $
"""

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

from Testing import ZopeTestCase

import DateTime

ZopeTestCase.installProduct('Five')
ZopeTestCase.installProduct('PythonScripts')
ZopeTestCase.installProduct('StandardCacheManagers')
ZopeTestCase.installProduct('RAMCacheManagerPlus')


from Products.StandardCacheManagers.RAMCacheManager import RAMCacheManager
from Products.RAMCacheManagerPlus.interfaces import ICacheablePlus, ICacheableMarker

ps_body = '''\
return context.getval()
'''

class TestRAMCacheManagerPlus(ZopeTestCase.ZopeTestCase):

    def afterSetUp(self):
        self.folder._setObject('ramcachemanager', RAMCacheManager('ramcachemanager'))
        dispatcher = self.folder.manage_addProduct['PythonScripts']

        dispatcher.manage_addPythonScript('ps')
        self.ps = self.folder['ps']
        self.ps.ZCacheable_setManagerId('ramcachemanager')
        self.ps.ZPythonScript_edit('', ps_body)

        dispatcher.manage_addPythonScript('getval')
        self.getval = self.folder['getval']
        self.getval.ZPythonScript_edit('', 'return 1')

    def testCaching(self):
        # Test something
        self.failUnless(self.ps.ZCacheable_isCachingEnabled())
        self.assertEqual(self.ps(), 1)
        self.getval.ZPythonScript_edit('', 'return 2')
        self.assertEqual(self.ps(), 1)
        ICacheablePlus(self.ps).invalidate(keywords_req={'AUTHENTICATED_USER':''}, partial=0)
        self.assertEqual(self.ps(), 1)
        ICacheablePlus(self.ps).invalidate(keywords_req={'AUTHENTICATED_USER':''}, partial=1)
        self.assertEqual(self.ps(), 2)
        ICacheablePlus(self.ps).invalidate(partial=1)
        self.getval.ZPythonScript_edit('', 'return 3')
        self.assertEqual(self.ps(), 3)



def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestRAMCacheManagerPlus))
    return suite

if __name__ == '__main__':
    framework()

